package top.yuyufeng.web.servlet;

import top.yuyufeng.web.utils.MyImageUtil;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;
import java.util.Map;

/**
 * @author yuyufeng
 * @date 2018/1/22
 */
@WebServlet(name = "ValidImageLocationServlet", urlPatterns = "/validImageLocation")
public class ValidImageLocationServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Map<String,String> map = (Map<String, String>) session.getAttribute("validMap");

        if(map == null){
            Writer writer = response.getWriter();
            writer.write("非法获取");
            writer.flush();
            return;
        }

        int x = Integer.valueOf(map.get("location").split(",")[0]);
        int y = Integer.valueOf(map.get("location").split(",")[1]);

        try {
            java.awt.Rectangle rectangle = new java.awt.Rectangle(x, y, 80, 80);
            OutputStream os = response.getOutputStream();
            MyImageUtil.cutImage(new File("D://test//upload/"+map.get("image")),os,rectangle);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
